//
//  ConsultListViewController.m
//  homeland
//
//  Created by caesar on 14-8-4.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "ConsultListViewController.h"
#import "ConsultListService.h"

@interface ConsultListViewController ()
@property (strong, nonatomic) CustomPullToRefresh *ptr;
@end

@implementation ConsultListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self configVC];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    
    [self.tableView reloadData];
//    self.tableView .dataSource = self;
    self.navigationController.navigationBar.hidden = NO;
}

- (BOOL)containsString:(NSString *)key aString:(NSString *)aString{
    if ([key isEqualToString:aString]) {
        return YES;
    }
	NSRange range = [[key lowercaseString] rangeOfString:[aString lowercaseString]];
	return range.location != NSNotFound;
}

- (BOOL)equalString:(NSString *)key aString:(NSString *)aString{
    if ([key isEqualToString:aString]) {
        return YES;
    }
    return NO;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Config
-(void)configVC{
    self.dataService = [[ConsultListService alloc]init];
    self.dataService.delegate = self;
//    self.dataService.dataItem = dataItem;
    self.dataService.paramDic = _config;
    //开启下拉刷新
    self.ptr = [[CustomPullToRefresh alloc]initWithScrollView:self.tableView delegate:self];
    //开始读取数据
    [self startLoadingView];
    [self.dataService loadDataWithKey:nil];
}



#pragma mark - Table
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return _dataService.dataList?[_dataService.dataList count]:0;
    } else {
        return 0;
    }
}
//赋值
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self.dataService heightForCellAtRow:indexPath];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellName = [_config objectForKey:@"ConsultListCellTableViewCell"];
    
 
    Class cellClass =  NSClassFromString(cellName);
    if (!cellClass) {
        return nil;
    }
    static NSString *cellIdentifier = @"ListViewCell";
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:cellName owner:self options:nil]objectAtIndex:0];
    }
    [cell setBackgroundColor:[UIColor clearColor]];
    //调用service对cell赋值
    [_dataService configCell:cell atIndex:indexPath];
    return cell;
}
//跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = [self.dataService getTargetConfigForCellAtIndex:indexPath];
    if (!dic) {
        return;
    }
    
   
    ConfigableViewController *vc = [_dataService getCellTargetViewControllerForName:[_config objectForKey:@"targetClassName"]];
    vc.config = dic;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - Data Service
-(void)loadDataFinished{
    
  
    [self stopLoadingView];
    [self.ptr endRefresh];
    [self.tableView reloadData];
}

-(void)loadDataFaild:(NSString *)msg{
    [self stopLoadingView];
    [self.ptr endRefresh];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"错误" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

-(void)setEnableToLoadMore:(BOOL)enable{
    self.ptr.enablePullToBottom = enable;
}

#pragma mark - PullToRefresh Method
//捕获到拉动事件
-(void)customPullToRefreshShouldRefresh:(CustomPullToRefresh *)ptr withDirection:(MSRefreshDirection)direction{
    if (direction == MSRefreshDirectionTop) {
        if (self.searchView.textField.text == 0  ) {
            [self.dataService loadDataWithKey:nil];
        }
    }
    if (direction == MSRefreshDirectionBottom  ) {
        if (self.searchView.textField.text == 0) {
            [self.dataService loadMoreDataWithKey:nil];
        }
    }
}

@end
